import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../common/flatPanel.dart';
import '../models/topicItem.dart';

class TopicItemDialog extends StatefulWidget {
  TopicItemDialog({Key key, this.item}) : super(key: key);

  @required
  final TopicItem item;

  @override
  _TopicItemPartsState createState() => _TopicItemPartsState();
}

class _TopicItemPartsState extends State<TopicItemDialog> {
  final _topic = TextEditingController();
  final _label = TextEditingController();
  final _detail = TextEditingController();
  final _addition = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();

  void _onSubmit() {
    var item = TopicItem(
      id: 0,
      topic: _topic.text,
      label: _label.text,
      detail: _detail.text,
      addition: _addition.text,
    );
    Navigator.pop(context, item);
  }

  Future<void> _onPaste() async {
    var data = await Clipboard.getData(Clipboard.kTextPlain);
    if (data.text.isNotEmpty) _detail.text = data.text;
  }

  @override
  void initState() {
    super.initState();
    if (widget.item.id != null && widget.item.id != 0) {
      _topic.text = widget.item.topic;
      _label.text = widget.item.label;
      _detail.text = widget.item.detail;
      _addition.text = widget.item.addition;
    } else {
      if (widget.item.topic.isNotEmpty) _topic.text = widget.item.topic;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width / 2,
      height: MediaQuery.of(context).size.height / 2,
      child: FlatPanel(
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                controller: _topic,
                decoration: InputDecoration(
                  hintText: '主题',
                  prefixIcon: Icon(Icons.title),
                ),
                validator: (v) => v.trim().length > 0 ? null : '主题不能为空',
              ),
              TextFormField(
                controller: _label,
                decoration: InputDecoration(
                  hintText: '名称',
                  prefixIcon: Icon(Icons.label_important),
                ),
                validator: (v) => v.trim().length > 0 ? null : '名称不能为空',
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: _detail,
                      decoration: InputDecoration(
                        hintText: '详述',
                        prefixIcon: Icon(Icons.content_paste),
                      ),
                      validator: (v) => v.trim().length > 0 ? null : '详述不能为空',
                    ),
                  ),
                  IconButton(
                    icon: Icon(Icons.paste),
                    onPressed: _onPaste,
                  ),
                ],
              ),
              TextFormField(
                controller: _addition,
                decoration: InputDecoration(
                  hintText: '附注',
                  prefixIcon: Icon(Icons.tag),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                        child: Text('清空', style: TextStyle(fontSize: 24)),
                        onPressed: () {
                          (_formKey.currentState as FormState).reset();
                        }),
                  ),
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                      child: Text(widget.item.id == 0 ? '提交' : '保存',
                          style: TextStyle(fontSize: 24, color: Colors.blue)),
                      onPressed: () {
                        if ((_formKey.currentState as FormState).validate())
                          _onSubmit();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

Future<TopicItem> showTopicItemDialog(BuildContext context,
    {TopicItem item}) async {
  return await showDialog<TopicItem>(
      context: context,
      builder: (context) {
        return Dialog(child: TopicItemDialog(item: item));
      });
}
